package com.han.bom.tests;

import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.han.bom.Answer;
import com.han.bom.Exam;
import com.han.bom.ExamResponse;
import com.han.bom.MultipleChoice;
import com.han.bom.PracticeExam;
import com.han.bom.Question;
import com.han.bom.RealExam;
import com.han.bom.Student;
import com.han.bom.TrueOrFalse;

public class StudentTest {

	private Student student = null;
	private ArrayList<Question> questions = null;
	
	@Before
	public void setUp() throws Exception {
		questions = new ArrayList<Question>();
		student = new Student(150);
		student.setUsername("userS150");
		student.setPassword("pass");
		student.setFirstName("Timothy");
		student.setLastName("Burgh");
		
		for(int i=0;i<10;i++){
			Question question = null;
			
			if(i%2==0){
				question = new MultipleChoice();
			}else{
				question = new TrueOrFalse();
			}
			questions.add(question);
		}
		
		for(int i=0;i<5;i++){
			Exam exam = null;
			
			if(i%2==0){
				exam = new RealExam("exam"+i,questions);
				exam.setExamID("E"+(i+1)+"RE");
			}else{
				exam = new PracticeExam("exam"+i,questions);
				exam.setExamID("E"+(i+1)+"PE");
			}
			student.getExamsToTake().add(exam);
			
			student.takeExam(exam);
		}
	}

	@After
	public void tearDown() throws Exception {
		student = null;
	}

	@Test
	public void testSearchExamExam() {
		for(int i=0;i<5;i++){
			Exam exam = null;
			
			if(i%2==0){
				exam = new RealExam("exam"+i,questions);
				exam.setExamID("E"+(i+1)+"RE");
			}else{
				exam = new PracticeExam("exam"+i,questions);
				exam.setExamID("E"+(i+1)+"PE");
			}
			assertEquals(exam, student.searchExam(exam));
		}
	}

	@Test
	public void testSearchExamString() {
		ArrayList<Exam> exams = new ArrayList<Exam>();
		
		for(int i=0;i<5;i++){
			Exam exam = null;
			
			if(i%2==0){
				exam = new RealExam("exam"+i,questions);
				exam.setExamID("E"+(i+1)+"RE");
			}else{
				exam = new PracticeExam("exam"+i,questions);
				exam.setExamID("E"+(i+1)+"PE");
			}
			exams.add(exam);
		}
		
		assertEquals(exams.get(0), student.searchExam(exams.get(0).getExamID()));
		assertEquals(exams.get(3), student.searchExam(exams.get(3).getExamID()));
		assertEquals(null, student.searchExam("E50RE"));
	}

	@Test
	public void testSearchExamResponse() {
		ExamResponse newer = new ExamResponse(student.searchExam("E1RE"), student.getUserID(), new ArrayList<Answer>());		
		assertEquals(newer, student.searchExamResponse("E1RE"));
		assertEquals(null, student.searchExamResponse("E20RE"));
	}

	@Test
	public void testTakeExam() {
		Exam exam = student.searchExam("E1RE");
		Exam exam2 = student.searchExam("E15RE");
		ExamResponse newer2 = new ExamResponse(exam, student.getUserID(), new ArrayList<Answer>());
		
		assertEquals(newer2, student.takeExam(exam));
		assertEquals(null, student.takeExam(exam2));
	}

}
